package com.jinkeen.xiaxie.keyboard

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import android.view.MotionEvent
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import com.jinkeen.xiaxie.R

/**
 * @description: TODO
 * @author whz
 * @date 2021/11/2 17:44
 * @version 1.0
 */

@SuppressLint("AppCompatCustomView")
class PasswordEdit3 : EditText {

    // 画笔
    private var mPaint: Paint? = null

    // 一个密码所占的宽度
    private var mPasswordItemWidth = 0

    // 密码的个数默认为6位数
    private var mPasswordNumber = 6

    // 背景边框颜色
    private var mBgColor = Color.parseColor("#d1d2d6")

    // 背景边框大小
    private var mBgSize = 1

    // 背景边框圆角大小
    private var mBgCorner = 0

    // 分割线的颜色
    private var mDivisionLineColor = mBgColor

    // 分割线的大小
    private var mDivisionLineSize = 1

    // 密码圆点的颜色
    private var mPasswordColor = mDivisionLineColor

    // 密码圆点的半径大小
    private var mPasswordRadius = 4


    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet!!, 0)
    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        initPaint()
        initAttrbiuteSet(context, attributeSet)
        //设置输入模式为密码
        inputType = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD
        //不显示光标
        isCursorVisible = true
    }

    private fun initPaint() {
        mPaint = Paint()
        mPaint!!.isAntiAlias = true
        mPaint!!.isDither = true
    }

    private fun initAttrbiuteSet(context: Context, attributeSet: AttributeSet) {
        var array = context.obtainStyledAttributes(attributeSet, R.styleable.PasswordEditText)
        //获取大小
        mDivisionLineSize = array.getDimension(
            R.styleable.PasswordEditText_divisionLineSize,
            dip2px(mDivisionLineSize)
        ).toInt()
        mPasswordRadius = array.getDimension(
            R.styleable.PasswordEditText_passwordRadius,
            dip2px(mPasswordRadius)
        )
            .toInt()
        mBgSize =
            array.getDimension(R.styleable.PasswordEditText_bgSize, dip2px(mBgSize)).toInt()
        mBgCorner = array.getDimension(R.styleable.PasswordEditText_bgCorner, 0f).toInt()
        // 获取颜色
        // 获取颜色
        mBgColor = array.getColor(R.styleable.PasswordEditText_bgColor, mBgColor)
        mDivisionLineColor =
            array.getColor(R.styleable.PasswordEditText_divisionLineColor, mDivisionLineColor)
        mPasswordColor =
            array.getColor(R.styleable.PasswordEditText_passwordColor, mDivisionLineColor)
        array.recycle()
    }


    private fun dip2px(dip: Int): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dip.toFloat(),
            resources.displayMetrics
        )

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        var passwordWidth = width - (mPasswordNumber - 1) * mDivisionLineSize
        mPasswordItemWidth = passwordWidth / mPasswordNumber
        //绘制背景
        drawBg(canvas!!)
        //绘制分割线
        drawDivisionLine(canvas!!)
        //绘制密码
        drawHidePassword(canvas!!)
    }

    /**
     * 绘制背景
     */
    private fun drawBg(canvas: Canvas) {
        mPaint?.color = mBgColor
        //设置画笔为空心
        mPaint?.style = Paint.Style.STROKE
        mPaint?.strokeWidth = mBgSize.toFloat()
        var rectF = RectF(
            mBgSize.toFloat(), mBgSize.toFloat(), (getWidth() - mBgSize).toFloat(),
            (getHeight() - mBgSize).toFloat()
        )
        //如果没有设置圆角，就画矩形
        if (mBgCorner == 0) {
            canvas.drawRect(rectF, mPaint!!)
        } else {
            canvas.drawRoundRect(rectF, mBgCorner.toFloat(), mBgCorner.toFloat(), mPaint!!)
        }

    }

    private fun drawHidePassword(canvas: Canvas) {
        val passwordLength = text.length
        mPaint!!.color = mPasswordColor
        // 设置画笔为实心
        // 设置画笔为实心
        mPaint!!.style = Paint.Style.FILL
        for (i in 0 until passwordLength) {
            val cx =
                i * mDivisionLineSize + i * mPasswordItemWidth + mPasswordItemWidth / 2 + mBgSize
            canvas.drawCircle(
                cx.toFloat(), (height / 2).toFloat(), mPasswordRadius.toFloat(),
                mPaint!!
            )
        }
    }

    private fun drawDivisionLine(canvas: Canvas) {
        mPaint!!.strokeWidth = mDivisionLineSize.toFloat()
        mPaint!!.color = mDivisionLineColor
        for (i in 0 until mPasswordNumber - 1) {
            val startX = (i + 1) * mDivisionLineSize + (i + 1) * mPasswordItemWidth + mBgSize
            canvas.drawLine(
                startX.toFloat(), mBgSize.toFloat(), startX.toFloat(), (height - mBgSize).toFloat(),
                mPaint!!
            )
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return super.onTouchEvent(event)
    }
}